﻿#include "widget_setting.h"
#include "ui_widget_setting.h"
#include <QTextCodec>
#include <QDebug>
#include <QMessageBox>
#include "serial_func.h"

widget_setting::widget_setting(QWidget *parent) :
    QWidget(parent),
    ui(new Ui::widget_setting)
{
    ui->setupUi(this);
    setAttribute(Qt::WA_QuitOnClose,false);

    setting_config = new QSettings(INICONFIG, QSettings::IniFormat, 0);
    setting_config->setIniCodec("GB2312");
    setWindowTitle(tr("选项"));

    //输入限制
    ui->comboBox_BaudRate->setValidator(new QIntValidator(0,999999,this));
    ui->comboBox_devaddr->setValidator(new QIntValidator(0,99,this));
    ui->comboBox_packageLen->setValidator(new QIntValidator(1,255,this));
    ui->lineEdit_overtime->setValidator(new QIntValidator(1,99,this));
    ui->comboBox_fontsize->setValidator(new QIntValidator(1,99,this));
    ui->lineEdit_tabsize->setValidator(new QIntValidator(1,99,this));
    //读传输选项
    ui->comboBox_port->addItems(serial_func::findAvailablePort());
    readConfig(ui->comboBox_port, "PORT");
    readConfig(ui->comboBox_BaudRate, "BaudRate");
    readConfig(ui->comboBox_devaddr, "DevAddr");
    readConfig(ui->comboBox_packageLen, "PakLen");
    readConfig(ui->lineEdit_overtime, "OverTime");
    //读字体选项
    QFont iniFont;
    iniFont.setFamily(setting_config->value("Font").toString());
    ui->fontComboBox->setCurrentFont(iniFont);
    readConfig(ui->comboBox_fontsize, "FontSize");
    readConfig(ui->lineEdit_tabsize, "TabSize");
    //读取语法高亮选项
    readConfig(ui->checkBox_highlight, "HighLighter");
}

widget_setting::~widget_setting()
{
    delete ui;
    delete setting_config;
}
//读取配置 QComboBox
void widget_setting::readConfig(QComboBox *item,QString key)
{
    int index = item->findText(
                setting_config->value(key).toString());
    if(index >= 0){
        item->setCurrentIndex(index);
    }else{
        item->addItem(setting_config->value(key).toString());
        readConfig(item, key);
    }
}
//读取配置 QLineedit
void widget_setting::readConfig(QLineEdit *item,QString key)
{
    item->setText(setting_config->value(key).toString());
}
//读取配置 QCheckBox
void widget_setting::readConfig(QCheckBox *item,QString key)
{
    item->setChecked(setting_config->value(key).toBool());
}
void widget_setting::on_setting_save_clicked()
{
    if(checkSetting(1) == false){
        return;
    }

    on_setting_apply_clicked();
    close();
}

void widget_setting::on_setting_apply_clicked()
{
    if(checkSetting(1) == false){
        return;
    }

    setting_config->setValue("PORT", ui->comboBox_port->currentText());
    setting_config->setValue("BaudRate", ui->comboBox_BaudRate->currentText());
    setting_config->setValue("DevAddr", ui->comboBox_devaddr->currentText());
    setting_config->setValue("PakLen", ui->comboBox_packageLen->currentText());
    setting_config->setValue("OverTime", ui->lineEdit_overtime->text());

    setting_config->setValue("Font", ui->fontComboBox->currentFont().family());
    setting_config->setValue("FontSize", ui->comboBox_fontsize->currentText());
    setting_config->setValue("TabSize", ui->lineEdit_tabsize->text());
    setting_config->setValue("HighLighter", ui->checkBox_highlight->isChecked());
    emit sig_setting_changed();
}
//检查设置
bool widget_setting::checkSetting(int warn)
{
    QString warnString;
    //串口设置
    if(ui->comboBox_port->currentText().isEmpty()){
        warnString = "串口号 输入有误";
    }else if(ui->comboBox_BaudRate->currentText().isEmpty()
             ||ui->comboBox_BaudRate->currentText().toInt() <= 0){
        warnString = "波特率 输入有误";
    }else if(ui->comboBox_devaddr->currentText().isEmpty()){
        warnString = "设备地址 输入有误";
    }else if(ui->comboBox_packageLen->currentText().isEmpty()
             ||ui->comboBox_packageLen->currentText().toInt() <= 0
             ||ui->comboBox_packageLen->currentText().toInt() > 255){
        warnString = "传输包大小 输入有误";
    }else if(ui->lineEdit_overtime->text().isEmpty()
            ||ui->lineEdit_overtime->text().toInt() <= 0){
        warnString = "超时时间 输入有误";
    }
    //字体设置
    if(ui->fontComboBox->currentText().isEmpty()){
        warnString = "字体 输入有误";
    }else if(ui->comboBox_fontsize->currentText().isEmpty()
            ||ui->comboBox_fontsize->currentText().toInt() <= 0){
        warnString = "字号 输入有误";
    }else if(ui->lineEdit_tabsize->text().isEmpty()
            ||ui->lineEdit_tabsize->text().toInt() <= 0){
        warnString = "tab缩进 输入有误";
    }

    if(!warnString.isEmpty()){
        if(warn == 1){
            QMessageBox::warning(this, tr("警告"), warnString + tr(",请检查设置。"));
        }
        return false;
    }
    return true;
}
